package com.whenjun.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ifootbook.util.FootBookException;
import com.whenjun.dto.kq.KqConfigGetLeaveDTO;
import com.whenjun.dto.kq.KqConfigGetLeaveReq;
import com.whenjun.dto.kq.KqConfigSetLeaveReq;
import com.whenjun.dto.person.IscPersonDTO;
import com.whenjun.entity.IscDoor;
import com.whenjun.entity.IscPerson;
import com.whenjun.entity.KqDepartmentGroupPerson;
import com.whenjun.entity.KqLocationLeave;
import com.whenjun.mapper.KqLocationLeaveMapper;
import com.whenjun.service.IKqLocationLeaveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whenjun.util.SaTokenUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-02-28
 */
@Service
public class KqLocationLeaveServiceImpl extends MPJBaseServiceImpl<KqLocationLeaveMapper, KqLocationLeave> implements IKqLocationLeaveService {

    @Transactional
    public void KqConfigAddLeave(KqConfigSetLeaveReq kqConfigSetLeaveReq) {
        KqLocationLeave kqLocationLeave = this.lambdaQuery()
                .eq(KqLocationLeave::getLocationId, kqConfigSetLeaveReq.getLocationId())
                .eq(KqLocationLeave::getIndexCode, kqConfigSetLeaveReq.getIndexCode())
                .eq(KqLocationLeave::getState, false)
                .one();

        if (kqLocationLeave != null) {
            throw new FootBookException("已添加该考勤点，请勿重复添加");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqLocationLeave = new KqLocationLeave();
        kqLocationLeave.setLocationId(kqConfigSetLeaveReq.getLocationId());
        kqLocationLeave.setIndexCode(kqConfigSetLeaveReq.getIndexCode());
        kqLocationLeave.setState(false);
        kqLocationLeave.setCreateTime(now);
        kqLocationLeave.setCreateUser(userId);

        this.save(kqLocationLeave);
    }

    @Transactional
    public void KqConfigRmLeave(KqConfigSetLeaveReq kqConfigSetLeaveReq) {
        KqLocationLeave kqLocationLeave = this.lambdaQuery()
                .eq(KqLocationLeave::getLocationId, kqConfigSetLeaveReq.getLocationId())
                .eq(KqLocationLeave::getIndexCode, kqConfigSetLeaveReq.getIndexCode())
                .eq(KqLocationLeave::getState, false)
                .one();

        if (kqLocationLeave == null) {
            throw new FootBookException("人员分组-考勤点 关系错误");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqLocationLeave.setState(true);
        kqLocationLeave.setUpdateTime(now);
        kqLocationLeave.setUpdateUser(userId);

        this.updateById(kqLocationLeave);
    }

    @Transactional
    public List<KqConfigGetLeaveDTO> KqConfigGetLeave(KqConfigGetLeaveReq kqConfigGetLeaveReq) {
        MPJLambdaWrapper<KqLocationLeave> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(KqLocationLeave.class)
                .select(IscDoor::getName)
                .leftJoin(IscDoor.class, IscDoor::getIndexCode, KqLocationLeave::getIndexCode)
                .eq(KqLocationLeave::getLocationId, kqConfigGetLeaveReq.getLocationId())
                .eq(KqLocationLeave::getState, false)
                .orderByDesc(KqLocationLeave::getCreateTime)
        ;

        List<KqConfigGetLeaveDTO> kqConfigGetLeaveDTOS = this.getBaseMapper().selectJoinList(KqConfigGetLeaveDTO.class, wrapper);

        return kqConfigGetLeaveDTOS;
    }
}
